

import 'package:auto_size_text/auto_size_text.dart';
import 'package:extra_hittest_area/extra_hittest_area.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:furaha_mkopo/commonTool/fm_color_tool.dart';
import 'package:furaha_mkopo/commonTool/fm_language_tool.dart';
import 'package:furaha_mkopo/commonTool/fm_screen_tool.dart';
import 'package:furaha_mkopo/commonUI/fm_custon_nav_bar_widget.dart';
import 'package:furaha_mkopo/page/order/fm_order_sub_page.dart';

class FMOrderPage extends StatefulWidget {

  final int index;

  final bool isIdentify;

  const FMOrderPage({super.key,required this.index,required this.isIdentify});

  @override
  State<FMOrderPage> createState() => _FMOrderPageState();
}

class _FMOrderPageState extends State<FMOrderPage> with SingleTickerProviderStateMixin {

  late TabController _tabController;

  int selectIndex = 0;

  List<String> list = [
    "6","7","5","4"
  ];

  @override
  void initState() {
    super.initState();
    selectIndex = widget.index;
    _tabController = TabController(length: 4, vsync: this);
    _tabController.index = selectIndex;

    _tabController.animation!.addListener(_handleTabIndexChangeTick);
  }

  void _handleTabIndexChangeTick() {
    if (_tabController.index.toDouble() == _tabController.animation!.value) {
      selectIndex = _tabController.index;
      setState(() {

      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor:FMColorTool.color("#54E041") ,
      body: Stack(
        children: [
          _bgWidget(),
          _navBarWidget(),
          _bottomWidget()
        ],
      )
    );
  }

  Widget _bgWidget() {
    return Positioned(
      top: 0,
      left: 0,
      right: 0,
      child: Container(
        height: 271,
        decoration: BoxDecoration(
          image: DecorationImage(
              image: AssetImage(FMLanguageTool.getAssets("assets/order/order_bg")),
            fit: BoxFit.cover
          ),
        ),
      ),
    );
  }

  Widget _navBarWidget() {
    return Positioned(
      top: FMScreenTool.topPadding(context),
      left: 0,
      right: 0,
      child: const FMCustomNavBarWidget(title: "")
    );
  }

  Widget _bottomWidget() {
    return Positioned(
      top: FMScreenTool.appBarHeight(context) + 110,
      left: 0,
      right: 0,
      bottom: 0,
      child: Column(
        children: [
          _menuWidget(),
          _contentWidget()
        ],
      ),
    );
  }

  Widget _menuWidget() {
    return Container(
      height: 55,
      padding: const EdgeInsets.only(top: 12,left: 16,right: 16),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          _menuSubWidget(FMLanguageTool.getText("Repayment"), 0,91),
          _menuSubWidget(FMLanguageTool.getText("Applying"), 1,72),
          _menuSubWidget(FMLanguageTool.getText("Finished"), 2,72),
          _menuSubWidget(FMLanguageTool.getText("All"), 3,24),
        ],
      ),
    );
  }

  Widget _contentWidget() {
    return Expanded(
      child: TabBarView(
        controller: _tabController,
        children: [
          FMOrderSubPage(listId: "6",isIdentify: widget.isIdentify),
          FMOrderSubPage(listId: "7",isIdentify: widget.isIdentify),
          FMOrderSubPage(listId: "5",isIdentify: widget.isIdentify),
          FMOrderSubPage(listId: "4",isIdentify: widget.isIdentify),
        ],
      ),
    );
  }

  Widget _menuSubWidget(String title,int index,double width) {
    return GestureDetectorHitTestWithoutSizeLimit(
      extraHitTestArea: const EdgeInsets.all(15),
      onTap: (){
        setState(() {
          selectIndex = index;
        });
        _tabController.index = index;
      },
      child: SizedBox(
        width: width,
        child: Column(
          children: [
            AutoSizeText(
              title,
              maxLines: 1,
              style: TextStyle(
                fontSize: index == selectIndex ? 16 : 15,
                fontWeight: index == selectIndex ? FontWeight.w700 : FontWeight.normal,
                color: index == selectIndex ? Colors.black : FMColorTool.blackColor
              ),
            ),
            const SizedBox(height: 2),
            Visibility(
              visible: index == selectIndex,
              child: Container(
                width: 45,
                height: 4,
                color: FMColorTool.color("#FF8800"),
              ),
            )
          ],
        ),
      ),
    );
  }
}
